package com.sx.manage.service.consumer;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sx.cache.Cache;
import com.sx.common.util.http.HttpPoolManager;
import com.sx.manage.biz.consumer.IMConsumerEditInfoBiz;
import com.sx.manage.biz.consumer.bank.IMConsumerBankMapBiz;
import com.sx.manage.biz.consumer.info.IMNetworkPersonConsumerBiz;
import com.sx.manage.biz.consumer.relation.IMConsumerDataRelationBiz;
import com.sx.manage.biz.data.auth.IMDataAuthorityBiz;
import com.sx.manage.biz.file.ICertificateAddressBiz;
import com.sx.manage.service.bank.IMBaseBankService;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.bank.IMConsumerBankMapService;
import com.sx.manage.service.city.IMBaseCityService;
import com.sx.manage.service.consumer.info.IMConsumerOperationInfoService;
import com.sx.manage.service.consumer.network.IMBasePersonConsumerNetworkService;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.constants.type.bank.MBindCardStatus;
import com.sx.model.constants.type.consumer.MConsumerDataRelationType;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerDataRelationVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerOperationInfoVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerNetworkVO;
import com.sx.model.manage.mybatis.vo.electronic.account.MElecAccountVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;

/**
 * 个人用户
 * 
 * @author lenovo
 *
 */
@Service("consumerWebServiceElecAccountImpl")
@Transactional(propagation = Propagation.REQUIRED)
public class MConsumerWebServiceElecAccountImpl {
	/** session超时设置 */
	public static final Integer HTTP_TIME_OUT = 50000;
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "redisCache")
	private Cache cache;

	// 用户信息新接口
	@Resource(name = "mNetworkPersonConsumerBizImpl")
	public IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl;

	// 系统参数配置
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	// 账户service
	//@Resource(name = "accountService")
	//private IAccountServiceInterface accountService;
	// 用户银行关系
	@Resource(name = "mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;
	// 用户银行关系
	@Resource(name = "consumerBankMapServiceImpl")
	private IMConsumerBankMapService consumerBankMapServiceImpl;
	// 银行信息
	@Resource(name = "mBaseBankServiceImpl")
	private IMBaseBankService mBaseBankServiceImpl;
	// 城市信息
	@Resource(name = "mBaseCityServiceImpl")
	private IMBaseCityService mBaseCityServiceImpl;
	// 用户关系数据
	@Resource(name = "mConsumerDataRelationBizImpl")
	private IMConsumerDataRelationBiz mConsumerDataRelationBizImpl;

	@Resource(name = "mBasePersonConsumerNetworkServiceImpl")
	private IMBasePersonConsumerNetworkService mBasePersonConsumerNetworkServiceImpl;
	// 用户银行信息biz
	@Resource(name = "mConsumerBankMapBizImpl")
	private IMConsumerBankMapBiz mConsumerBankMapBizImpl;

	@Resource(name = "httppoolmanager")
	private HttpPoolManager httpPoolManager;
	// 用户信息修改
	@Resource(name = "mConsumerEditInfoBizImpl")
	private IMConsumerEditInfoBiz mConsumerEditInfoBizImpl;
	// 数据下载权限申请
	@Resource(name = "dataAuthorityBizImpl")
	private IMDataAuthorityBiz dataAuthorityBizImpl;
	// 扩展信息
	@Resource(name = "consumerOperationInfoServiceImpl")
	private IMConsumerOperationInfoService consumerOperationInfoServiceImpl;

	@Resource(name = "webServiceUtils")
	private WebServiceUtils webServiceUtils;

	// 证件地址信息
	@Resource(name = "certificateAddressBizImpl")
	private ICertificateAddressBiz certificateAddressBizImpl;



	// 绑卡前的验证
	public ResultBooleanVO bindElecAccountValidate(MElecAccountVO accountVO) {
		//
		log.info("绑定的出入金卡前用户信息验证，accountVO={}", accountVO);
		if (null == accountVO) {
			log.info("用户没有开通直销银行consumer_id={}", accountVO);
			return new ResultBooleanVO(false, null, "用户没有开通直销银行");
		}
		if (StringUtils.isBlank(accountVO.getCard_no())) {
			log.info("用户直销银行没有绑定银行卡consumer_id={}", accountVO);
			return new ResultBooleanVO(false, null, "用户直销银行没有绑定银行卡");
		}
		//查询扩展信息
		MConsumerOperationInfoVO infoVO = consumerOperationInfoServiceImpl.queryByConsumerId(accountVO.getConsumer_id()).getObjVo();
		if(null==infoVO) {
			log.info("用户没有扩展信息，不能判断是否开通。consumer_id={}", accountVO);
			return new ResultBooleanVO(false, null, "用户没有扩展信息，不能判断是否开通。");
		}
		if(MBindCardStatus.BIND.equals(infoVO.getHuaxia02_bind_card_status())) {
			log.info("用户已经绑定出入金卡，请先解绑。huaxia02_bind_card_status={}", infoVO.getHuaxia02_bind_card_status());
			return new ResultBooleanVO(false, null, "用户已经绑定出入金卡，请先解绑。");
		}
		return new ResultBooleanVO();
	}

	



	// 解绑出金卡操作
	@SuppressWarnings("rawtypes")
	public ResultBooleanVO relieveBankOutCardOper(Long consumer_id, MElecAccountVO accountVO) {
		log.info("解绑用户银行卡consumer_id={},account_id={}", consumer_id, accountVO.getId());
		// 获取用户、银行信息
		MPersonalConsumerNetworkVO consumerVO = mNetworkPersonConsumerBizImpl.get_consumer_byId(consumer_id).getObjVo();
//		// 生成解绑出入金form
//		BindBankCardForm cardForm = ClassPackUtil.createRelieveElecAccount(consumerVO, accountVO, webServiceUtils.queryBranchAccountNo(consumer_id), webServiceUtils.queryTradeMemberCode(consumer_id));
//		try {
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(WebServiceUtil.bindBankOutCardUrl(), ContentType.JSON, null, HTTP_TIME_OUT,
//					JackJsonUtil.objToJson(cardForm), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info("解绑出入金结果 httpResult={}", httpResult.getHttpresult());
//				B2BQueryAccountRssultVO accountResult = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BQueryAccountRssultVO.class);
//				// 返回绑定关系不存在时，认定成功，这样就能继续资源，删除绑定信息的操作
//				if (!accountResult.isSuccess() && !MSystemConstants.HX_RELIEVE_RETURN_FALSE_PASS.equals(accountResult.getMsgcode())) {
//					log.info("解绑失败error={}", accountResult.getMsg());
//					return new ResultBooleanVO(false, "sx.consumer.bank.relieve.bank.out.card.fail", accountResult.getMsg());
//				} else {
//					return new ResultBooleanVO();
//				}
//			}
//		} catch (Exception e) {
//			log.error("解绑出金账户http异常error={}", e);
//		}
		return new ResultBooleanVO("sx.consumer.bank.relieve.bank.out.card.fail");
	}
	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ResultListVO<MConsumerBankMapVO> bindBankCardList(Long consumer_id) {
		log.debug("跳转到用户银行信息添加界面consumer_id={}", consumer_id);
		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.CONSUMER_PERSON_CHANNEL_TYPE).getObjVo();

		// 获取用户所有银行卡信息
		if (null != relationVO) {
			MConsumerBankMapForm bankForm = new MConsumerBankMapForm();
			bankForm.setConsumer_id(consumer_id);
			bankForm.setChannel_type(relationVO.getRelation_string_num());
			ResultListVO<MConsumerBankMapVO> result = this.mConsumerBankMapBizImpl.queryListByForm(bankForm);
			Long num = Long.valueOf(0);
			if (null != result.getData()) {
				num = Long.valueOf(result.getData().size());
			}
			result.setRecordsTotal(num);
			result.setRecordsFiltered(num);
			return result;

		} else {
			return new ResultListVO<>(new ArrayList<>(0), Long.valueOf(0), Long.valueOf(0));
		}
	}

}
